package com.hengxinguotong.zhihuichengjian.net;

import android.text.TextUtils;

import com.hengxinguotong.zhihuichengjian.utils.MimeUtil;

import java.io.File;
import java.util.Map;
import java.util.Set;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * TODO
 *
 * @author hjz
 * @version V1.0.0
 * @since 2018/8/30 16:25
 * <p>
 * 20181225
 */

public class BaseRequest
{
    protected static final String SERVER_ERROR = "系统异常";
    protected static final int SUCCESS_CODE = 200;

    private static final String BASE_URL = "http://221.234.44.78:81/escst_interface";

    /**
     * 创建Retrofit实例
     *
     * @param url
     * @return retrofit2.Retrofit
     * @author hjz
     * @since 2019/4/17 9:35
     */
    protected Retrofit createRetrofit(String url)
    {
        String realUrl = TextUtils.isEmpty(url) ? BASE_URL : url;
        return new Retrofit.Builder()
                .baseUrl(realUrl) //设置网络请求的Url地址
                .client(ClientFactory.create(realUrl))
                .addConverterFactory(GsonConverterFactory.create()) //设置数据解析器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    /**
     * 转multipart请求体
     *
     * @param params
     * @param files
     * @return okhttp3.MultipartBody
     * @author hjz
     * @since 2018/10/16 15:09
     */
    protected MultipartBody toMultipartBody(Map<String, String> params, Map<String, File> files)
    {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        Set<Map.Entry<String, String>> paramSet = params.entrySet();
        for (Map.Entry<String, String> entry : paramSet)
        {
            builder.addFormDataPart(entry.getKey(), entry.getValue());
        }
        Set<Map.Entry<String, File>> fileSet = files.entrySet();
        File file;
        for (Map.Entry<String, File> entry : fileSet)
        {
            file = entry.getValue();
            if (file != null && file.exists())
            {
                MediaType mediaType = MediaType.parse(MimeUtil.guessMimeType(file.getAbsolutePath()));
                RequestBody requestBody = RequestBody.create(mediaType, file);
                builder.addFormDataPart("filename", file.getName(), requestBody);
            }
        }
        return builder.build();
    }

    protected MultipartBody toMultipartBody(Map<String, String> params, File file)
    {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        Set<Map.Entry<String, String>> paramSet = params.entrySet();
        for (Map.Entry<String, String> entry : paramSet)
        {
            builder.addFormDataPart(entry.getKey(), entry.getValue());
        }
        if (file != null && file.exists())
        {
            MediaType mediaType = MediaType.parse(MimeUtil.guessMimeType(file.getAbsolutePath()));
            RequestBody requestBody = RequestBody.create(mediaType, file);
            builder.addFormDataPart("filename", file.getName(), requestBody);
        }
        return builder.build();
    }
}
